<template>
  <div class="app-container">
    <div class="filter-container">
      <el-row>
        <el-col :span="6">交易额：{{analysisData.amount}}</el-col>
        <el-col :span="6">
          利润：

          <el-tag v-if="analysisData.profit < 0" effect="dark" type="success">￥{{ analysisData.profit }}</el-tag><el-tag effect="dark" type="danger" v-else >￥{{ analysisData.profit }}</el-tag>
        </el-col>
        <el-col :span="6">费用：{{analysisData.cost}}</el-col>
        <el-col :span="6">交易量：{{analysisData.quantity}}</el-col>
      </el-row>      
      
      
      
    </div>
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="账户" prop="inventorID">
          <el-input v-model="filterForm.inventorID" clearable placeholder="账户" style="width: 200px;" class="filter-item"  />
        </el-form-item>
        <el-form-item label="主力合约" prop="localSymbol">
          <el-select v-model="filterForm.localSymbol" placeholder="请选择" @keyup.enter.native="handleFilter">
            <el-option
            v-for="item in symbolOptions"
            :key="item.code"
            :label="item.fullname"
            :value="item.code">
            </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="合约" prop="symbol">
          <el-input v-model="filterForm.symbol" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="交易时间" prop="tradeTimes">
          <el-radio-group v-model="filterCycleScale" @input="handleCycleButton">
            <el-radio-button label="1">今日</el-radio-button>
            <el-radio-button label="2">近7日</el-radio-button>
            <el-radio-button label="3">近30日</el-radio-button>
            <el-radio-button label="4">本周</el-radio-button>
            <el-radio-button label="5">本月</el-radio-button>
            <el-radio-button label="6">本季度</el-radio-button>
            <el-radio-button label="7">今年</el-radio-button>
          </el-radio-group>
          <el-date-picker 
          clearable
            v-model="tradeTimes"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <el-button @click="handleFilter()">搜索</el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      stripe
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" width="200px">
        <template slot-scope="{row}">
          <span>{{ row.tradeID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合约" prop="id" sortable="custom" align="center" width="140">
        <template slot-scope="{row}">
          <span>{{ row.symbol }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" min-width="150px">
      <el-table-column label="开仓" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.openPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平仓" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column label="数量" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.cost }}</span>
        </template>
      </el-table-column>
      <el-table-column label="利润" min-width="150px">
        <template slot-scope="{row}">
          <span><el-tag v-if="row.profit < 0" effect="dark" type="success">￥{{ row.profit }}</el-tag><el-tag effect="dark" type="danger" v-else >￥{{ row.profit }}</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.createdAt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getList" />

  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination
import {getSymbol} from '@/utils/symbol' // secondary package based on el-pagination
import {getShipmentsList} from '@/api/position/shipments'
import {getSymbolList} from '@/api/knowledge/symbol'

export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      
      socket:"",
      tableKey: 0,
      list: null,
      windowHeight:"800",
      total: 0,
      symbolOptions:[],
      listLoading: false,
      connected:"loading",
      analysisData:{},
      tradeTimes:["2024-04-01 00:00:00","2024-05-05 23:59:59"],
      filterCycleScale:1,
      filterForm: {
        page: 1,
        size: 10,
        beginDate:"2024-04-01",
        endDate:"2024-05-05",
        localSymbol:"",
        status:5,
        title: undefined,
        sort: '+id'
      }
    }
  },
  watch:{
    'tradeTimes'(newTimes,oldTimes){
      console.log(newTimes[0],newTimes[1])
      this.filterForm.beginDate = newTimes[0]
      this.filterForm.endDate = newTimes[1]
    }
  },
  created() {
    let curDate = new Date()
    
    let curMonth = curDate.getMonth()+1
    if(curMonth < 10){
      curMonth = "0"+curMonth
    }
    this.filterForm.beginDate = curDate.getFullYear()+"-"+curMonth+"-"+curDate.getDate()+" 00:00:00"
    this.filterForm.endDate = curDate.getFullYear()+"-"+curMonth+"-"+curDate.getDate()+" 23:59:59"
    this.tradeTimes[0] = this.filterForm.beginDate
    this.tradeTimes[1] = this.filterForm.endDate
    this.getList()
    this.getSymbolList()
  },
  methods: {
    getList:function(){
      let _this = this;
      getShipmentsList(_this.filterForm).then((response) => {
        console.log("getList",response)
          if (response.code === 200) {
            this.list = response.data.list
            this.total = response.data.total
            this.analysisData = response.data.analysisData
            console.log(this.analysisData.profit)
            this.analysisData.profit = Number(this.analysisData.profit).toFixed(2)
            console.log(this.analysisData.profit)
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    getSymbolList:function(){
      let _this = this;
      console.log("期权",_this.filterForm)
      getSymbolList({
        "page":1,
        "size":100
      }).then((response) => {
        console.log("getSymbolList",response)
          if (response.code === 200) {
            this.symbolOptions = response.data.list
          } else {
            _this.$message({
              message: response.msg,
              type: 'error'
            })
          }
      })
    },
    __toDate:function(cTime){
      let curDate = new Date(cTime);
      let fullYear = curDate.getFullYear()    
      let curMonth = curDate.getMonth()+1
      if(curMonth < 10){
        curMonth = "0"+curMonth
      }
      let curDay = curDate.getDate()
      if(curDay < 10){
        curDay = "0"+curDay
      }
      return fullYear+"-"+curMonth+"-"+curDay;
    },
    handleCycleButton:function(scale){
      console.log("时间类型",scale)
      let curDate = new Date();
      let curTime = curDate.getTime()
      let curYear = curDate.getFullYear()
      let curMonth = curDate.getMonth()+1
      let curWeek = curDate.getDay()
      console.log(this.__toDate(curTime))
      //今日
      if(scale == 1){
        this.filterForm.beginDate = this.__toDate(curTime)+" 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      //近7日
      if(scale == 2){
        this.filterForm.beginDate = this.__toDate(curTime-60*60*24*7*1000)+" 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
          
      }
      //近30日
      if(scale == 3){
        this.filterForm.beginDate = this.__toDate(curTime-60*60*24*30*1000)+" 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      //本周
      if(scale == 4){
        this.filterForm.beginDate = this.__toDate(curTime-60*60*24*(curWeek-1)*1000)+" 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      //本月
      if(scale == 5){
        if(curMonth < 10){
          curMonth = "0"+curMonth
        }
        this.filterForm.beginDate = curYear+"-"+curMonth+"-01 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      //本季度
      if(scale == 6){
        let startMonth = 1
        let season = Math.ceil(curMonth/3)
        if(season == 1){
          startMonth = '01'
        }
        if(season == 2){
          startMonth = '04'
        }
        if(season == 3){
          startMonth = '07'
        }
        if(season == 4){
          startMonth = '10'
        }
        this.filterForm.beginDate = curYear+"-"+startMonth+"-01 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      //本年
      if(scale == 7){
        this.filterForm.beginDate = this.__toDate(curTime-60*60*24*30*1000)+" 00:00:00"
        this.filterForm.endDate = this.__toDate(curTime)+" 23:59:59"
      }
      this.tradeTimes = [this.filterForm.beginDate,this.filterForm.endDate]
      console.log(this.filterForm)
      console.log(this.tradeTimes)
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange:function(){

    }
  }
}
</script>
<style>
.filter { float: left; width: 160px; padding: 20px; min-height: 600px; }
.article { float: left; width: 800px; }
.article-item { width: 800px; height: 200px; margin-right: 25px; }
.article-item-text { float: left; width:600px; }
.article-item-icon { float: right; width:200px; height:150px; padding-left: 50px; }
.article-item-icon img { width:100%; height:100%; border-radius: 10px; }
</style>